package com.tengju.bff.interfaces.manage.crm.dashborad.assembler;

import com.tengju.bff.interfaces.manage.crm.dashborad.vo.DashboardBrandMarketSortVO;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.DashboardSECGoodsMarketSortVO;
import com.tengju.bff.interfaces.shared.Formatter;
import com.tengju.data.domain.model.clickhouse.dashboard.DashBoardMarketInfo;
import com.tengju.data.domain.model.clickhouse.dashboard.MonthQuarterEnum;
import com.tengju.data.domain.shared.Month;
import com.tengju.data.domain.shared.Quarter;
import org.apache.commons.collections.CollectionUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * @Date: 2021/4/29 11:18
 * @Description: 
 */
public class DashboardMarketSortAssembler {

    public static List<DashboardBrandMarketSortVO> assemblerMarketSortVOList(List<DashBoardMarketInfo> infoList, LocalDate localDate,
                                                                             MonthQuarterEnum monthQuarterEnum) {
        if (CollectionUtils.isEmpty(infoList)) {
           return new ArrayList<>();
        }
        // 如果获取当月数据 返回数据不是当前月 直接过滤
        if (monthQuarterEnum == MonthQuarterEnum.CURRENT_MONTH) {
            infoList = infoList.stream()
                    .filter(v -> new Month(LocalDate.parse(v.getVisitDate())).sameMonth(localDate))
                    .collect(Collectors.toList());

        // 如果获取当前季度数据 返回数据不是当前季度 直接过滤
        } else if (monthQuarterEnum == MonthQuarterEnum.CURRENT_QUARTER) {
            infoList = infoList.stream()
                    .filter(v -> new Quarter(LocalDate.parse(v.getVisitDate())).sameQuarter(localDate))
                    .collect(Collectors.toList());

        // 如果获取上月数据 返回数据不是上月 直接过滤
        } else {
            infoList = infoList.stream()
                    .filter(v -> new Month(LocalDate.parse(v.getVisitDate())).sameMonth(localDate.minusMonths(1)))
                    .collect(Collectors.toList());
        }

        List<DashboardBrandMarketSortVO> voList = new ArrayList<>();
        int brank = 1;
        for (DashBoardMarketInfo info : infoList) {
            DashboardBrandMarketSortVO vo = new DashboardBrandMarketSortVO();
            vo.setBrank(brank);
            vo.setBrandName(info.getBrandName() == null ? "" : info.getBrandName());

            //使用逗号格式化金额
            vo.setSellAccount(Formatter.commaMoney(info.getSellAcount()));

            vo.setHodeRatio(String.format("%s",info.getHodeRatio() == null ? "0" : info.getHodeRatio()+"%"));
            vo.setBrandId(info.getBrandId().getValue());

            brank++;
            voList.add(vo);

        }
        return voList;


    }

    public static List<DashboardSECGoodsMarketSortVO> assemblerSECMarketSortVOList(List<DashBoardMarketInfo> infoList, LocalDate localDate,
                                                                                   MonthQuarterEnum monthQuarterEnum) {
        if (CollectionUtils.isEmpty(infoList)) {
            return new ArrayList<>();
        }

        // 如果获取当月数据 返回数据不是当前月 直接过滤
        if (monthQuarterEnum == MonthQuarterEnum.CURRENT_MONTH) {
            infoList = infoList.stream()
                    .filter(v -> new Month(LocalDate.parse(v.getVisitDate())).sameMonth(localDate))
                    .collect(Collectors.toList());

        // 如果获取当前季度数据 返回数据不是当前季度 直接过滤
        } else if (monthQuarterEnum == MonthQuarterEnum.CURRENT_QUARTER) {
            infoList = infoList.stream()
                    .filter(v -> new Quarter(LocalDate.parse(v.getVisitDate())).sameQuarter(localDate))
                    .collect(Collectors.toList());

        // 如果获取上月数据 返回数据不是上月 直接过滤
        } else {
            infoList = infoList.stream()
                    .filter(v -> new Month(LocalDate.parse(v.getVisitDate())).sameMonth(localDate.minusMonths(1)))
                    .collect(Collectors.toList());
        }


        List<DashboardSECGoodsMarketSortVO> voList = new ArrayList<>();
        int brank = 1;
        for (DashBoardMarketInfo info : infoList) {
            DashboardSECGoodsMarketSortVO vo = new DashboardSECGoodsMarketSortVO();
            vo.setBrank(brank);
            vo.setGoodsName(info.getBrandName() == null ? "" : info.getBrandName());

            vo.setSellAccount(Formatter.commaMoney(info.getSellAcount()));
            vo.setSpuId(info.getBrandId().getValue());

            brank++;
            voList.add(vo);
        }

        return voList;
    }
}
